package cn.iinda.electronicInfo.service.impl;

import cn.iinda.electronicInfo.dao.DicomJpgViewMapper;
import cn.iinda.electronicInfo.dao.DicomdataViewMapper;
import cn.iinda.electronicInfo.dao.DicominfoViewMapper;
import cn.iinda.electronicInfo.dao.ElectronicInfoMapper;
import cn.iinda.electronicInfo.model.*;
import cn.iinda.electronicInfo.service.ElectronicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version V3.0
 * @Title: ElectronicInfoServiceImpl
 * @Company: 成都影达科技有限公司
 * @Description: 描述
 * @author: 东进
 * @date 2018-01-26 9:59
 */
@Service
public class ElectronicInfoServiceImpl implements ElectronicInfoService {

    @Autowired
    private DicomdataViewMapper dicomdataViewMapper;
    @Autowired
    private DicominfoViewMapper dicominfoViewMapper;
    @Autowired
    private ElectronicInfoMapper electronicInfoMapper;
    @Autowired
    private DicomJpgViewMapper dicomJpgViewMapper;

    @Override
    public ElectronicFilmResult findByPatientNum(String patientNum) {
        if (null == patientNum || patientNum.trim().equals(""))
            return ElectronicFilmResult.build(400, "传入参数不能为空");
        ElectronicInfoExample example = new ElectronicInfoExample();
        ElectronicInfoExample.Criteria criteria = example.createCriteria();
        criteria.andPatientNumEqualTo(patientNum);
        List<ElectronicInfo> list = electronicInfoMapper.selectByExample(example);
        if (null != list && list.size() > 0)
            return ElectronicFilmResult.ok(list);
        else
            return ElectronicFilmResult.build(400, "无此数据");
    }

    @Override
    public ElectronicFilmResult findByPatientName(String patientName) {
        if (null == patientName || patientName.trim().equals(""))
            return ElectronicFilmResult.build(400, "传入参数不能为空");
        ElectronicInfoExample example = new ElectronicInfoExample();
        ElectronicInfoExample.Criteria criteria = example.createCriteria();
        criteria.andPatientNameEqualTo(patientName);
        List<ElectronicInfo> list = electronicInfoMapper.selectByExample(example);
        if (null != list && list.size() > 0)
            return ElectronicFilmResult.ok(list.get(0));
        else
            return ElectronicFilmResult.build(400, "无此数据");
    }

    @Override
    public ElectronicFilmResult findByPhoneNum(String phoneNum) {
        if (null == phoneNum || phoneNum.trim().equals(""))
            return ElectronicFilmResult.build(400, "传入参数不能为空");
        ElectronicInfoExample example = new ElectronicInfoExample();
        ElectronicInfoExample.Criteria criteria = example.createCriteria();
        criteria.andPhoneNumEqualTo(phoneNum);
        System.out.println(1);
        Date start = new Date();
        List<ElectronicInfo> list = electronicInfoMapper.selectByExample(example);
        Date end = new Date();
        System.out.println(2);
        System.out.println(end.getTime() - start.getTime());
        ArrayList<ElectronicInfoCustom> infoCustoms = new ArrayList<>();
        for (ElectronicInfo e : list) {
            ElectronicInfoCustom ec = electronicInfoTranElectronicInfoCustom(e);
            infoCustoms.add(ec);
        }
        if (null == list || list.size() < 1)
            return ElectronicFilmResult.build(400, "无此数据");
        return ElectronicFilmResult.ok(infoCustoms);
    }

    @Override
    public ElectronicFilmResult findById(Integer id) {
        if (null == id || id == 0)
            return ElectronicFilmResult.build(400, "传入参数不能为空");
        ElectronicInfoExample example = new ElectronicInfoExample();
        ElectronicInfoExample.Criteria criteria = example.createCriteria();
        criteria.andResultIdEqualTo(id);
        List<ElectronicInfo> list = electronicInfoMapper.selectByExample(example);
        if (null != list && list.size() > 0)
            return ElectronicFilmResult.ok(electronicInfoTranElectronicInfoCustom(list.get(0)));
        else
            return ElectronicFilmResult.build(400, "无此数据");
    }

    @Override
    public ElectronicFilmResult getDicomCount(Integer studyInfoId) {
        if (studyInfoId == null)
            return ElectronicFilmResult.build(400, "存入数据不能为空");
        DicominfoViewExample example = new DicominfoViewExample();
        DicominfoViewExample.Criteria criteria = example.createCriteria();
        criteria.andStudyinfoidEqualTo(studyInfoId);
        Date start = new Date();
        List<DicominfoView> list = dicominfoViewMapper.selectByExample(example);
        Date end = new Date();
        System.out.println(end.getTime() - start.getTime());
        if (null != list && list.size() > 0)
            return ElectronicFilmResult.ok(list.get(0));
        else
            return ElectronicFilmResult.build(400, "系统中无数据");
    }

    @Override
    public ElectronicFilmResult findDicomdataByStudyId(Integer studyId) {
        if (null == studyId)
            return ElectronicFilmResult.build(400, "传入参数不能为空");
        DicomdataViewExample example = new DicomdataViewExample();
        DicomdataViewExample.Criteria criteria = example.createCriteria();
        criteria.andStudyinfoidEqualTo(studyId);
        Date start = new Date();
        List<DicomdataView> list = dicomdataViewMapper.selectByExample(example);
        Date end = new Date();
        System.out.println(end.getTime() - start.getTime());
        if (null == list || list.size() < 1)
            return ElectronicFilmResult.build(401, "表中无此数据");
        return ElectronicFilmResult.ok(list);
    }

    @Override
    public ElectronicFilmResult findJpgByResultId(Integer resultId) {
        if (null == resultId)
            return ElectronicFilmResult.build(400, "传入参数不能为空");
        DicomJpgViewExample example = new DicomJpgViewExample();
        DicomJpgViewExample.Criteria criteria = example.createCriteria();
        criteria.andResultIdEqualTo(resultId);
        List<DicomJpgView> list = dicomJpgViewMapper.selectByExample(example);
        if (null != list && list.size() > 0)
            return ElectronicFilmResult.ok(list);
        else
            return ElectronicFilmResult.build(400, "无此信息");
    }

    ElectronicInfoCustom electronicInfoTranElectronicInfoCustom(ElectronicInfo e) {
        ElectronicInfoCustom ec = new ElectronicInfoCustom();
        if (e.getPatientName() == null || e.getPatientName().trim().equals(""))
            ec.setPatientName("无");
        else
            ec.setPatientName(e.getPatientName());
        ec.setResultId(e.getResultId());
        if (e.getPhoneNum() == null || e.getPhoneNum().trim().equals(""))
            ec.setPhoneNum("无");
        else
            ec.setPhoneNum(e.getPhoneNum());
        if (e.getImpression() == null || e.getImpression().trim().equals("无"))
            ec.setImpression("无");
        else
            ec.setImpression(e.getImpression());
        ec.setReportFlag(e.getReportFlag());
        if (e.getSummary() == null || e.getSummary().trim().equals(""))
            ec.setSummary("无");
        else
            ec.setSummary(e.getSummary());
        if (e.getReportDr() == null || e.getReportDr().trim().equals(""))
            ec.setReportDr("无");
        else
            ec.setReportDr(e.getReportDr());
        ec.setStudyId(e.getStudyId());
        if (e.getModality() == null || e.getModality().trim().equals(""))
            ec.setModality("无");
        else
            ec.setModality(e.getModality());
        // TODO: 2018-02-26  
//        if (e.getCreateTime().contains("."))
//            ec.setCreateTime(e.getCreateTime().substring(0, e.getCreateTime().indexOf(".")));
//        if (e.getReportTime().contains("."))
//            ec.setReportTime(e.getReportTime().substring(0, e.getReportTime().indexOf(".")));
        if (e.getReportTime() != null)
            ec.setReportTime(e.getReportTime());
        if (e.getExamDate().length() > 7)
            ec.setCheckTime(e.getExamDate());
        if (e.getStudyPoint() == null || e.getStudyPoint().trim().equals(""))
            ec.setStudyPoint("无");
        else
            ec.setStudyPoint(e.getStudyPoint());
        if (e.getBasicMedical() == null || e.getBasicMedical().trim().equals("") || e.getBasicMedical().trim().equals("null"))
            ec.setBasicMedical("无");
        else
            ec.setBasicMedical(e.getBasicMedical());
        if (e.getPatientNum() == null || e.getPatientNum().trim().equals(""))
            ec.setPatientNum("无");
        else
            ec.setPatientNum(e.getPatientNum());
        if (e.getVerifyDr() == null || e.getVerifyDr().trim().equals(""))
            ec.setVerifyDr("无");
        else
            ec.setVerifyDr(e.getVerifyDr());
        if (null == e.getIsPositive())
            ec.setPositive("阴性");
        else if (e.getIsPositive().equals("1"))
            ec.setPositive("阳性");
        else
            ec.setPositive("阴性");
        if (null == e.getFinding() || e.getFinding().trim().equals(""))
            ec.setFinding("无");
        else
            ec.setFinding(e.getFinding());
        if (e.getPatientGender() == null || e.getPatientGender().trim().equals(""))
            ec.setPatientGender("未知");
        else
            ec.setPatientGender(e.getPatientGender());
        if (null == e.getPatientAge() || e.getPatientAge().trim().equals(""))
            ec.setPatientAge("无");
        else
            ec.setPatientAge(e.getPatientAge());
        return ec;
    }
}
